//
//  FCCollectionViewCell.m
//  FengShuiCompass
//
//  Created by 张德广 on 2019/3/14.
//  Copyright © 2019 compass. All rights reserved.
//

#import "FCCollectionViewCell.h"

@implementation FCCollectionViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    if (self) {
        // Create a image view
        self.backgroundColor = [UIColor clearColor];
        
        [self.compassView addSubview:self.imageView];
        [self.compassView addSubview:self.verticalLabel];
        [self.compassView addSubview:self.horizonLabel];
        [self.compassView addSubview:self.compassImage];
        [self.compassView addSubview:self.lockImageView];
        
        self.autoresizesSubviews = YES;
    }
    return self;
}

- (void)prepareForReuse {
    [super prepareForReuse];
    
    [self.scrollView setZoomScale:1.0];
}

#pragma mark- scroll
- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
    
    CGFloat offsetX = (scrollView.bounds.size.width > scrollView.contentSize.width)?
    (scrollView.bounds.size.width - scrollView.contentSize.width) * 0.5 : 0.0;
    
    CGFloat offsetY = (scrollView.bounds.size.height > scrollView.contentSize.height)?
    (scrollView.bounds.size.height - scrollView.contentSize.height) * 0.5 : 0.0;
    
    self.compassView.center = CGPointMake(scrollView.contentSize.width * 0.5 + offsetX,
                                          scrollView.contentSize.height * 0.5 + offsetY);
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    
    return self.compassView;
}


#pragma mark ------ getter
- (FCScrollerView *)scrollView {
    if (!_scrollView) {
        _scrollView = [[FCScrollerView alloc] initWithFrame:CGRectMake(50*kWidthValue, 0, self.frame.size.width - 100*kWidthValue, self.frame.size.height )];
        _scrollView.delegate = self;
        _scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleWidth;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.showsVerticalScrollIndicator = NO;
        [_scrollView setMinimumZoomScale:1.0];
        [_scrollView setMaximumZoomScale:2.0];
        [_scrollView setZoomScale:1.0];
    }
    return _scrollView;
}

- (UIView *)compassView {
    if (!_compassView) {
        _compassView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, FC_SCREEN_WIDTH, FC_SCREEN_WIDTH)];
        _compassView.center = CGPointMake(self.frame.size.width/2, self.frame.size.height/2);
    }
    return _compassView;
}

- (UIImageView *)imageView {
    if (!_imageView) {
        _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.compassView.frame.size.width, self.compassView.frame.size.width)];
        _imageView.center = CGPointMake(self.compassView.frame.size.width/2, self.compassView.frame.size.height/2);
    }
    return _imageView;
}

- (UIImageView *)lockImageView {
    if (!_lockImageView) {
        _lockImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.compassView.frame.size.width, self.compassView.frame.size.width)];
        _lockImageView.center = CGPointMake(self.compassView.frame.size.width/2, self.compassView.frame.size.height/2);
    }
    return _lockImageView;
}

- (UIImageView *)compassImage {
    if (!_compassImage) {
        _compassImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 80*kWidthValue, 80*kWidthValue)];
        _compassImage.center = CGPointMake(self.compassView.frame.size.width/2, self.compassView.frame.size.height/2);
    }
    return _compassImage;
}

- (UILabel *)verticalLabel {
    if (!_verticalLabel) {
        _verticalLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 1.0, self.compassView.frame.size.width - 9*kWidthValue)];
        _verticalLabel.center = CGPointMake(self.compassView.frame.size.width/2, self.compassView.frame.size.height/2);
        _verticalLabel.backgroundColor = [UIColor redColor];
    }
    return _verticalLabel;
}

- (UILabel *)horizonLabel {
    if (!_horizonLabel) {
        _horizonLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.compassView.frame.size.width - 9*kWidthValue, 1.0)];
        _horizonLabel.center = CGPointMake(self.compassView.frame.size.width/2, self.compassView.frame.size.height/2);
        _horizonLabel.backgroundColor = [UIColor redColor];
    }
    return _horizonLabel;
}

@end
